<?php
/**
 * 代金券
 */
namespace App\Controller\Member;
use Common\Controller\App\CommonController;
use Common\Model\MicroShopCouponsModel;

class CouponController extends CommonController{

    /**
     * 代金券列表
     */
    public function index(){
        $cpns_key = I('post.cpns_key');
        $status = I('post.status')?I('post.status'):0;  //1已使用  0未使用 2已过期
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):20;
        $num = ($page-1)*$limit;
        $member_id = session('member_id');
        $model = new MicroShopCouponsModel();
        $time = time();
        if($status==0){
            $list = $model->where("member_id=$member_id and status=$status and UNIX_TIMESTAMP(start_time)<$time and UNIX_TIMESTAMP(end_time)>$time")->limit($num,$limit)->select();

        }
        if($status==1){
            $list = $model->where("member_id=$member_id and status=$status")->limit($num,$limit)->select();

        }
        if($status==2){
            $list = $model->where("member_id=$member_id and  UNIX_TIMESTAMP(end_time)<$time")->limit($num,$limit)->select();

        }
        if($cpns_key){
            $list = $model->where("cpns_key='$cpns_key'")->select();
        }
        foreach($list as $k=>$v){
            $day = getdays($time,$v['end_time']);
            $list[$k]['day'] = $day;
        }
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }


}